package sg.edu.ntu.sci.blackboard.agilix.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import javax.xml.parsers.DocumentBuilderFactory;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.w3c.dom.Document;

/**
 *
 * @author ngocgiang
 */
public class IOUtil {

    private static Log log = LogFactory.getLog(IOUtil.class);

    public static Document parseXmlFile(String filename, boolean validating) {
        try {
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            factory.setValidating(validating);
            Document doc = factory.newDocumentBuilder().parse(new File(filename));
            return doc;
        } catch (Exception e) {
            log.error("Error in parsing document", e);
        }
        return null;
    }

    public static String readFile(String filename) throws IOException {
        StringBuffer res = new StringBuffer();
        BufferedReader in = new BufferedReader(new FileReader(filename));
        String str = null;
        while ((str = in.readLine()) != null)
            res.append(str);
        in.close();
        return res.toString();
    }
}
